package me.zhengjie.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.aop.log.Log;
import me.zhengjie.domain.WxpayConfig;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.service.WxpayService;
import me.zhengjie.service.dto.WxpayDTO;
import me.zhengjie.service.dto.WxpayQueryCriteria;
import me.zhengjie.utils.WxpayUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zxw
 * @date 2019-11-22
 */
@Slf4j
@RestController
@RequestMapping("/api/wxPay")
@Api(tags = "工具：wx支付")
public class WxController {

    private final WxpayUtils wxpayUtils;

    private final WxpayService wxpayService;

    private static final String ENTITY_NAME = "wxpay";

    public WxController(WxpayUtils wxpayUtils, WxpayService wxpayService){
        this.wxpayUtils = wxpayUtils;
        this.wxpayService = wxpayService;
    }

    @Log("查询wx数据")
    @ApiOperation("查询wx数据")
    @GetMapping
    //@PreAuthorize("@el.check('user:list','dept:list')")
    public ResponseEntity getWxpays(WxpayQueryCriteria criteria, Pageable pageable){
        // 数据
        //criteria.setIds(dataScope.getDeptIds());
        Map<String, Object> wxpayDTOS = wxpayService.queryAll(criteria,pageable);
        return new ResponseEntity<>(wxpayDTOS, HttpStatus.OK);
    }

    @Log("新增wx数据")
    @ApiOperation("新增wx数据")
    @PostMapping
    //@RequestMapping("add")
    //@PreAuthorize("@el.check('wxPay:add')")
    public ResponseEntity create(@Validated @RequestBody WxpayConfig resources){
//        if (resources.getId() != null) {
//            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
//        }
        return new ResponseEntity<>(wxpayService.create(resources),HttpStatus.CREATED);
    }

}
